package utils

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gcache"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/util/gconv"
	"miaoyu/internal/service"
)

/*
@Time : 2023/6/8 9:55
@Author : 56766
@File : cache.logic.go
*/
type sCache struct {
	Cache *gcache.Cache
}

func init() {
	service.RegisterCache(NewCache())
}
func NewCache() *sCache {

	cache := sCache{
		Cache: gcache.New(),
	}
	var isRedis bool
	config, _ := g.Cfg().Get(gctx.New(), "cache.redis")
	isRedis = gconv.Bool(config)
	if isRedis {

		redis := g.Redis("cache")
		cache.Cache.SetAdapter(gcache.NewAdapterRedis(redis))
	}
	return &cache
}
func (s *sCache) GetCache() *gcache.Cache {
	return s.Cache
}
